package cliente;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public class URLReaderContracs {

	static URL pagina;
	static BufferedReader in;
	static String entradaOriginal, entrada, entrada2, entrada3, entrada4; 

	static String desc="Desc";
	static String rare="\"Rare";
	static String itemType="ItemType";
	
	static String [] typeDesc, typeRare, typeItemType, card;
	static int idcontract;  
	static Image imagecarta;
	static URL carta;

	ImageIcon bronceNoRare = new ImageIcon("broncenorare.png");
	ImageIcon silverNoRare = new ImageIcon("silvernorare.png");
	ImageIcon goldNoRare = new ImageIcon("goldnorare.png");
	ImageIcon bonceRare = new ImageIcon("broncerare.png");
	ImageIcon silverRare = new ImageIcon("silverrare.png");
	ImageIcon goldRare = new ImageIcon("goldrare.png");
	ImageIcon staffBronceNoRare = new ImageIcon("staffbroncenorare.png");
	ImageIcon staffSilverNoRare = new ImageIcon("staffsilvernorare.png");
	ImageIcon staffGoldNoRare = new ImageIcon("staffgoldnorare.png");
	ImageIcon staffBronceRare = new ImageIcon("staffbroncerare.png");
	ImageIcon staffSilverRare = new ImageIcon("staffsilverrare.png");
	ImageIcon staffGoldRare = new ImageIcon("staffgoldrare.png");

	static JPanel panelImagenContract = new JPanel();
	static JLabel label1;
	
	static void seleccionarContracts (){
		int contador = GuiCliente.modeloTablaBuscar.getRowCount();

		if(contador!=0){
			for(int x=0;x<contador;x++){
				GuiCliente.modeloTablaBuscar.removeRow(0);
			}
			contador=0;
		}
		
		typeDesc = new String [5001013];
		typeRare = new String [5001013];
		typeItemType = new String [5001013];
		card = new String [5001013];
		
		for(int x=5001001;x<5001013;x++){
			
		try {
			pagina = new URL("http://cdn.content.easports.com/fifa/fltOnlineAssets/2013/fut/items/web/"+x+".json");
			in = new BufferedReader (new InputStreamReader(pagina.openStream()));
			entradaOriginal = in.readLine();
			entrada=entradaOriginal;
			idcontract=x;

		}catch (IOException e) {e.printStackTrace();}

	    	entrada = entrada.substring(entrada.indexOf(desc)+desc.length(),entrada.length());
	    	entrada2 = entrada.substring(entrada.indexOf(rare)+rare.length(),entrada.length());
	    	entrada3 = entrada.substring(entrada.indexOf(itemType)+itemType.length(),entrada.length());
	    	
	    	typeDesc[x]=entrada.substring(entrada.indexOf(":")+2 , entrada.indexOf(",")-1);	    	
	    	typeRare[x]=entrada2.substring(entrada2.indexOf(":")+2 , entrada2.indexOf(",")-1);
	    	typeItemType[x]=entrada3.substring(entrada3.indexOf(":")+2 , entrada3.indexOf("}")-1);

	    	switch(typeRare[x]){
	    	case "0":
	    		if(idcontract==5001001){
		    		card[x]="5001001.png";
	    		}
	    		else if(idcontract==5001002){
		    		card[x]="5001002.png";
		    	}
	    		else if(idcontract==5001003){
		    		card[x]="5001003.png";
		    	}
		   		if(idcontract==5001007){
		    		card[x]="5001007.png";
		   		}
		   		else if(idcontract==5001008){
		    		card[x]="5001008.png";
		    	}
		   		else if(idcontract==5001009){
		    		card[x]="5001009.png";
		    	}
	    		break;
	    	case "1":
	    		if(idcontract==5001004){
		    		card[x]="5001004.png";
	    		}
		   		else if(idcontract==5001005){
		    		card[x]="5001005.png";
		    	}
		    	else if(idcontract==5001006){
		    		card[x]="5001006.png";
			   	}
		    	if(idcontract==5001010){
		    		card[x]="5001010.png";
		    	}
		    	else if(idcontract==5001011){
		    		card[x]="5001011.png";
			    }
		    	else if(idcontract==5001012){
		    		card[x]="5001012.png";
			    }
	    		break;
	    	}
		
	    	Object nuevaFila[]= {Integer.toString(idcontract),"",typeItemType[x],typeDesc[x],typeRare[x]};
	    	GuiCliente.modeloTablaBuscar.addRow(nuevaFila);	    
	    try {
	    	
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		}
	}

	static void panelImagenContract(int lead){	

		panelImagenContract.setLayout(null);
		panelImagenContract.removeAll();
		ImageIcon carta=new ImageIcon(card[5001001+lead]);

		label1 = new JLabel(carta);
		URLReaderPlayers.panelImagenPlayers.setVisible(false);
		panelImagenContract.setVisible(false);
		panelImagenContract.setBounds(535, 20, 210, 280);GuiCliente.panelSearchPlayers.add(panelImagenContract);panelImagenContract.setVisible(true);
		
        label1.setBounds(0, 0, 210, 280);panelImagenContract.add(label1);label1.setVisible(true);	
       
	}

}